﻿package
{
	import gameStage;
	import User;
	
	public class Game
	{
		private var mTurn:int;
		private var mType:int;
		private var mHand1:String;
		private var mHand2:String;
		private var mHand3:String;
		private var mHand4:String;
		//Player Status: READY, PASS, DISC, 1ST, 2ND, 3RD, 4TH
		private var mStatus1:String;
		private var mStatus2:String;
		private var mStatus3:String;
		private var mStatus4:String;
		private var mTable:String;
		
		public function Game()
		{
			mTurn = 0;
			mType = 0;
			mHand1 = "";
			mHand2 = "";
			mHand3 = "";
			mHand4 = "";
			mStatus1 = "";
			mStatus2 = "";
			mStatus3 = "";
			mStatus4 = "";
			mTable = "";
		}
		
		public function gameState(inTurn:int, inType:int, inH1:String, inH2:String, inH3:String, inH4:String,
								  inS1:String, inS2:String, inS3:String, inS4:String, inTable:String):void
		{
			mTurn = inTurn;
			mType = inType;
			mHand1 = inH1;
			mHand2 = inH2;
			mHand3 = inH3;
			mHand4 = inH4;
			mStatus1 = inS1;
			mStatus2 = inS2;
			mStatus3 = inS3;
			mStatus4 = inS4;
			mTable = inTable;
		}
		
		// Params: n/a
		// Return: void
		// DESC: Increments the turn value. Recursively checks for DISC status
		public function nextTurn():void
		{
			if(mTurn == 4) { mTurn = 1; } 
			else { mTurn++; } 
			//checks for player DISC status
			if(getStatus(mTurn - 1) == "DISC")
			{
				nextTurn();
			}
		}
		
		// Params: n/a
		// Return: Boolean
		// DESC: Checks to see if anyone is READY; If yes, returns True.
		public function checkREADY():Boolean
		{
			var temp:Boolean = false;
			for(var i:int=0; i<4; i++)
			{
				if(getStatus(i) == "READY" && main.ourUserID != i)
				{
					temp = true;
				}
			}
			return temp;
		}
		
		// Params: n/a
		// Return: void
		// DESC: Resets everyone's PASS status to READY.
		public function resetStatus():void
		{
			for(var i:int=0; i<4; i++)
			{
				if(getStatus(i) == "PASS")
				{
					setStatus(i, "READY");
				}
			}
		}
		
		// Params: n/a
		// Return: String of rank
		// DESC: Determines the appropriate rank for the player.
		public function assignRank():String
		{
			var rank:int = 0;
			for(var i:int=0; i<4; i++)
			{
				if(getStatus(i) == "1ST" && rank < 1) { rank = 1; } 
				else if(getStatus(i) == "2ND" && rank < 2) { rank = 2; } 
				else if(getStatus(i) == "3RD" && rank < 3) { rank = 3; } 
			}
			if(rank == 0) { return "1ST"; } 
			else if(rank == 1) { return "2ND"; } 
			else if(rank == 2) { return "3RD"; } 
			else if(rank == 3) { return "4TH"; }
			else { return "ERROR" }
		}
		
		// Params: n/a
		// Return: Boolean
		// DESC: Determines if the game is over. The game is over when 3 player's hands are
		//       empty or if the player is disconnected.
		public function isGameOver():Boolean
		{
			var temp:int = 0;
			for(var i:int=0; i<4; i++)
			{
				if(getHand(i) == "") { temp++; } 
				else if(getStatus(i) == "DISC") { temp++; } 
			}
			if(temp>2) { return true; } 
			else { return false; } 
		}
		
		public function getGameStateXML():String
		{
			return "<turn>"+mTurn+"</turn><type>"+mType+"</type><hand1>"+mHand1+"</hand1><status1>"+mStatus1+"</status1><hand2>"+mHand2+"</hand2><status2>"+mStatus2+"</status2><hand3>"+mHand3+"</hand3><status3>"+mStatus3+"</status3><hand4>"+mHand4+"</hand4><status4>"+mStatus4+"</status4><table>"+mTable+"</table>";
		}
		
		public function getHand(inID:int):String
		{
			if(inID == 0) { return mHand1; }
			else if(inID == 1) { return mHand2; }
			else if(inID == 2) { return mHand3; }
			else if(inID == 3) { return mHand4; }
			else { return "ERROR"; }
		}
		
		public function getStatus(inID:int):String
		{
			if(inID == 0) { return mStatus1; }
			else if(inID == 1) { return mStatus2; }
			else if(inID == 2) { return mStatus3; }
			else if(inID == 3) { return mStatus4; }
			else { return "ERROR"; }
		}
		public function setHand(inID:int, inHand:String):void
		{
			if(inID == 0) { mHand1 = inHand; }
			else if(inID == 1) { mHand2 = inHand; }
			else if(inID == 2) { mHand3 = inHand; }
			else if(inID == 3) { mHand4 = inHand; }
			else { trace ("ERROR"); }
		}
		
		public function setStatus(inID:int, inStat:String):void
		{
			if(inID == 0) { mStatus1 = inStat; }
			else if(inID == 1) { mStatus2 = inStat; }
			else if(inID == 2) { mStatus3 = inStat; }
			else if(inID == 3) { mStatus4 = inStat; }
			else { trace ("ERROR"); }
		}
		
		public function get turn():int { return mTurn; }
		public function get type():int { return mType; }
		public function get hand1():String { return mHand1; }
		public function get hand2():String { return mHand2; }
		public function get hand3():String { return mHand3; }
		public function get hand4():String { return mHand4; }
		public function get status1():String { return mStatus1; }
		public function get status2():String { return mStatus2; }
		public function get status3():String { return mStatus3; }
		public function get status4():String { return mStatus4; }
		public function get table():String { return mTable;	}
		
		public function set turn(inTurn:int) { mTurn = inTurn; }
		public function set type(inType:int) { mType = inType; }
		public function set hand1(inH1:String) { mHand1 = inH1; }
		public function set hand2(inH2:String) { mHand2 = inH2; }
		public function set hand3(inH3:String) { mHand3 = inH3; }
		public function set hand4(inH4:String) { mHand4 = inH4; }
		public function set status1(inS1:String) { mStatus1 = inS1; }
		public function set status2(inS2:String) { mStatus2 = inS2; }
		public function set status3(inS3:String) { mStatus3 = inS3; }
		public function set status4(inS4:String) { mStatus4 = inS4; }
		public function set table(inTable:String) { mTable = inTable; }
	}
}